package com.pig4cloud.pig.qc.controller;

import cn.hutool.core.util.ArrayUtil;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.OrderCodeUtil;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.pig.qc.entity.LabelEntity;
import com.pig4cloud.pig.qc.entity.OrderEntity;
import com.pig4cloud.pig.qc.service.OrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 *
 * @author QC
 * @date 2025-07-28 11:12:14
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/carOrder")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class OrderController {

    private final OrderService orderService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('qc_carOrder_view')")
	public R getOrderPage(@ParameterObject Page page,
						  @Param("order") OrderEntity order){
		return R.ok(orderService.getOrderPage(page, order));
	}


    /**
     * 新增
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('qc_carOrder_add')")
    public R save(@RequestBody OrderEntity order) {
		order.setOrderCode(OrderCodeUtil.generateOrderCode("DD",4));
		order.setSalePerson(SecurityUtils.getUser().getUsername());
		orderService.updateInfo(order);
		return R.ok(orderService.save(order));
    }
	/**
	 * 客户姓名下拉菜单
	 */
	@GetMapping("/customer")
	public R CustomerNameList() {
		return R.ok(orderService.getCustomerNameList());
	}
	/**
	 *车辆编号下拉菜单
	 */
	@GetMapping("/carCode")
	public R<List<OrderEntity>> CarCodeList() {
		return R.ok(orderService.getCarCodeList());
	}

    /**
     * 导出excel 表格
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('qc_carOrder_export')")
    public List<OrderEntity> export(OrderEntity order, Long[] ids) {
        return orderService.list(Wrappers.lambdaQuery(order).in(ArrayUtil.isNotEmpty(ids), OrderEntity::getOrderId, ids));
    }


}
